package com.itcodai.service.impl;

import com.itcodai.service.AsyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AsyncServiceImpl implements AsyncService {

    @Resource
    private TestService testService;

    private static final Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class);

    // 将Service层的服务异步化
    // asyncServiceExecutor方法是前面ExecutorConfig.java中的方法名，
    // 表明executeAsync方法进入的线程池是asyncServiceExecutor方法创建的
    @Override
    @Async("asyncServiceExecutor")
    public void executeAsync(int index) {
        logger.info("start executeAsync task-{}", index);
        logger.info("threadName-{}", Thread.currentThread().getName());
        //System.out.println("异步线程要做的事情");
        //System.out.println("可以在这里执行批量插入等耗时的事情");
        doTask();
        logger.info("end executeAsync task-{}", index);

        testService.jmeterExecuteAsync(index);
    }

    // 做具体的任务
    public void doTask() {
        logger.info("threadName2-{}", Thread.currentThread().getName());
        try {
            Thread.sleep((long) (10 * (1 + Math.random()) * 1000L));
            //System.out.println("执行结束！");
            //logger.info("执行结束！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}